<?php
require_once 'Produit.class.php';
require_once 'DbConnector.class.php';
require_once  'classes/Ingredient.class.php';

class Pizza extends Produit
{
        public $ingredients= array();
        public $base;
        
        // Adds an ingredient
        public function addIngredient($ing_id)
        {
        	if(isset($this->id)&&($this->id != null))
        	{
        		$query = "INSERT INTO pizza_ingredients(
        		pizza_id, ingredient_id) VALUES ('".$this->id."', '".$ing_id."')";
        		if(!DbConnector::get()->exec($query)) throw new Exception("Could not insert ingredient, query : ".$query);
        	}
        }
        
        // Vide les ingrédients
        public function clearIngredients()
        {
        	if(isset($this->id)&&($this->id != null))
        	{
        		$query = "DELETE FROM pizza_ingredients WHERE pizza_id=".$this->id."";
        		DbConnector::get()->exec($query);
        	}
        }
        
        // Récupère TOUTE la liste de pizzas
        public static function getAll() {
                $query = "SELECT * FROM pizza";
                
                $dbc = DbConnector::get();
                $stmt = $dbc->prepare($query);
                $stmt->execute();
                $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
                $pizzArray = array();
                
                // Stockage des pizzas
                foreach($result as $pizza) {
                        $tmpZza = new Pizza($pizza['pizza_id'], $pizza['pizza_name'], $pizza['pizza_price'], $pizza['pizza_base']);
                        $pizzArray[] = $tmpZza;
                }
                return $pizzArray;
        }
        
        public static function getNomByID($idPizza){
        	$query = "SELECT pizza_name FROM pizza where pizza_id=".$idPizza;
        	$dbc = DbConnector::get();
        	$stmt = $dbc->prepare($query);
        	$stmt->execute();
        	$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        	if($result!=null){
        		return $result[0]['pizza_name'];
        	}
        }
        public static function getPrixByID($idPizza){
        	$query = "SELECT pizza_price FROM pizza where pizza_id=".$idPizza;
        	$dbc = DbConnector::get();
        	$stmt = $dbc->prepare($query);
        	$stmt->execute();
        	$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result[0]['pizza_price'];
        }
        
        public function save()
        {
        	// New pizza
        	if(empty($this->id) || ($this->id == 0))
        	{
        		$query = "INSERT INTO pizza(
            	pizza_name, pizza_price, pizza_base)
    				VALUES ('".$this->nom."', '".$this->prix."', '".$this->base."')";
        		DbConnector::get()->exec($query);
        		
        		// Name is unique...
        		$query = "SELECT pizza_id FROM pizza WHERE pizza_name='".$this->nom."'";
        		$stmt = DbConnector::get()->query($query);
        		$res = $stmt->fetch(PDO::FETCH_ASSOC);
        		
        		$this->id = $res['pizza_id']; 
        	}
        	// Pizza already exists
        	else
        	{
        		$query = "UPDATE pizza
   					SET  pizza_name='".$this->nom."', pizza_price='".$this->prix."', pizza_base='".$this->base."'
 				WHERE pizza_id = '".$this->id."'";
        		
        		DbConnector::get()->exec($query);	
        	}
        }
        
        public function __construct($id,$nom,$prix,$base){
        		parent::__construct($id, $nom, $prix);
                $this->base=$base;
                $this->setIngredients();
        }
        
        public function setIngredients()
        {
                if(isset($this->id))
                {
                        $query = "SELECT ingredient_id, ingredient_name, ingredient_stock,ingredient_prix
                                        FROM PIZZA_INGREDIENTS NATURAL JOIN INGREDIENT WHERE PIZZA_ID = '".$this->id."'";
                        $dbc = DbConnector::get();
                        $stmt = $dbc->prepare($query);
                        $stmt->execute();
                        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
                        
                        $this->ingredients = array();
                        
                        foreach ($result as $ingredient)
                        {
                                $tmp = new Ingredient($ingredient['ingredient_id'], $ingredient['ingredient_name'], $ingredient['ingredient_stock'],$ingredient['ingredient_prix']);
                                array_push($this->ingredients, $tmp);
                        }
                }
                        
        }
        public function setIngredientsTab($tab){
        	$this->ingredients=$tabs;
        }
        function getXML(){
                $xml = new DOMDocument;
                $xml->formatOutput = true;
                
                $pizza = $xml->createElement('pizza');
                $pizzaNode=$xml->appendChild($pizza);
                $pizzaNode->setAttribute('id',$this->id);
                
                $pizzaNode->appendChild(new DOMElement('nomPizza',$this->nom));
                $pizzaNode->appendChild(new DOMElement('base',$this->base));
                $pizzaNode->appendChild(new DOMElement('prix',$this->prix));
                
                $ingredients=$xml->createElement('ingredients');
                $ingredientsNode=$pizzaNode->appendChild($ingredients);
                $count=count($this->ingredients);
                for($i=0;$i<$count;$i++){
                        $ingredientsNode->appendChild($xml->importNode($this->ingredients[$i]->getXML(true),true));
                }
                $lstIngredients=Ingredient::getAll();
                $nbIngredientsTotal=count($lstIngredients);
                $ingredientsUnselected=array_udiff($lstIngredients, $this->ingredients,array('Ingredient','compare'));
				$count=count($ingredientsUnselected);
				if($count!=0){
					foreach($ingredientsUnselected as $ingredient){
						$ingredientsNode->appendChild($xml->importNode($ingredient->getXML(false),true));
					}
				}
                return $xml->getElementsByTagName('pizza')->item(0);
        }
  	    public function getBase(){
        	return $this->base;
        }
        public function getListIngredients(){
        	return $this->ingredients;
        }
}
?>

